module Sol8
where 

import WWN

toBase :: Integral a => a -> a -> [Int]
toBase b n | b < 2 || b > 16 = error "base not in [2..16]"
           | n < 0           = error "negative argument"
           | otherwise       = reverse (toB b n)
    where 
    toB b n | n < b     = [toInt n]
            | otherwise = toInt (rem n b) : toB b (quot n b)

hex :: (Integral a) => a -> String
hex = showDigits . toBase 16

leq1 :: NatPair -> NatPair -> Bool
leq1 (m1,m2) (n1,n2) = (m1+n2) <= (m2+n1)
 
gt1 :: NatPair -> NatPair -> Bool
gt1 p1 p2  = not (p1 `leq1` p2)

periods :: [Rational] -> [Int]
periods xs = map periodLength xs 
  where 
  periodLength x = length (third (decForm x))
  third (_,_,c)  = c

